console.log('===>', $event)
const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
const form144 = this.pageObj.getApiWidgetByAliasName('form_144')
const change = this.getApiPage().getApiWidgetByAliasName('change')?.widgetObj;
const id = $event.equipmentCode
const options = {
    method: 'POST',
    headers: {
        "Content-type": "application/json",
    },
    body: JSON.stringify({ equipmentId: $event.equipmentCode })
}

function playCamera (camera, cb = () => {}) {
    const equipmentUnifyConfig = camera?.equipmentConfig?.equipmentUnifyConfig
    if (equipmentUnifyConfig) {
        const { onlineStatus } = equipmentUnifyConfig
        if (+onlineStatus !== 0) {
             window.layer?.msg('设备维修中！');
        } else {
            const option = { stream_path: camera.stream_path, equipmentCode: camera.equipmentCode,equipmentId: camera.equipmentCode, ...camera }
            widgets.widgetObj.sullivan.addPlayerWidthData(`${window.location.origin}/streamUrl/api/v1/stream/rtc/play`, option)

            cb()
        }
    }
}

if (widgets) {
    console.log(widgets)
    if ($event.deviceId && $event.channelId) {
        const ptzCtrl = this.pageObj.getApiWidgetByAliasName('ptz-ctrl')
        const optss = this.pageObj.getApiWidgetsByClassName('optss')
        const optsf = this.pageObj.getApiWidgetsByClassName('opts-form')
        const ptzBg = this.pageObj.getApiWidgetByAliasName('ptz-background')
        const $form = this.pageObj.getApiWidgetByAliasName('form_14')
        const _form = this.pageObj.retrievePlugin('base-forms')

        playCamera($event, () => {
            if (ptzCtrl) {
                ptzCtrl.showWidget()
                ptzBg.showWidget()
                form144.hideWidget()
                optss.forEach(v => v.showWidget())
                optsf.forEach(v => v.showWidget())
                ptzCtrl.widgetObj.setPtzController({
                    id: $event.deviceId,
                    channel: $event.channelId
                })
            }
            change._option.defaultKey = 'preset'
            change.render()
            _form.setFormData('form_14', { id })
            fetch('/IotDdevice/project-business-iot-service/hikvision/sdk/queryPresets', options)
                .then(res => res.json())
                .then(json => {
                    if (json.success) {
                        console.log('_form', _form)
                        const list = json.result.map(v => ({
                            ...v,
                            sourceName: v.name,
                            name: v.presetNum + '-' + v.name,
                            value: v.presetNum
                        }))
                        $form.widgetObj._option.options = {
                            list
                        }
                        this.pageObj.setVariables([{ name: 'device_list', value: list }])
                        $form.widgetObj.render()
                        console.log('data', _form.getFormData('form_14'))
                    } else {
                        window.layer?.msg('获取预置位失败！')
                        $form.widgetObj._option.options = { list: [] }
                        $form.widgetObj.render()
                    }
                    _form.setFormData('form_14', { id })
                })
        })

        console.log('$event: => ', ptzCtrl)

    } else {
        window.layer?.msg('摄像头未找到配置！');
    }

} else {
    window.layer?.msg('摄像头未找到！');
}
